﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Threading;
using System.Threading;

namespace inplayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        string polecenie,argument;
        StringBuilder OutputWindowText = new StringBuilder();
        Process process;
        Boolean clicked=false;
        public MainWindow()
        {
            InitializeComponent();
            gen_Command();
            textBox4.TextWrapping = TextWrapping.Wrap;
            radioButton1.Click += (sender, se) => gen_Command();
            radioButton2.Click += (sender, se) => gen_Command();
        }
        

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = getFilePath();
            gen_Command();
        }

        private string getFilePath()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            Nullable<bool> result = dlg.ShowDialog();



            // Get the selected file name and display in a TextBox

            if (result == true)
            {

                // Open document

                return dlg.FileName;

                

            }
            return "No file selected";

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox2.Text = getFilePath();
            gen_Command();
        }

        private void button3_Click_1(object sender, RoutedEventArgs e)
        {
            textBox3.Text = getFilePath();
            gen_Command();
        }
        private void gen_Command()
        {
            string subtitlesPart = " -sub \"" + textBox2.Text + "\" -utf8";
            string encodeParam,outputFileName;
            if (radioButton1.IsChecked == true)
            {
                encodeParam = " -oac mp3lame -ovc xvid -xvidencopts fixed_quant=3:threads=3";
                outputFileName = " -o \"" + textBox1.Text + "subt"+DateTime.Now.ToString().Replace(":","_") + ".avi\"";
                label6.Content = System.IO.Path.GetFileName(outputFileName.Substring(5,outputFileName.Length-6));
            }
            else
            {
                encodeParam = " -oac mp3lame -ovc x264 -x264encopts subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid=normal:weight_b"; 
                outputFileName = " -o \"" + textBox1.Text + "subt"+DateTime.Now.ToString().Replace(":","_") + ".avi\"";
                label6.Content = System.IO.Path.GetFileName(outputFileName.Substring(5, outputFileName.Length - 6))+".mp4";
            }
            polecenie = textBox3.Text; 
           argument = encodeParam+subtitlesPart+outputFileName+"  \"" + textBox1.Text+"\"";
           
            textBox4.Text = polecenie+argument;
           }

        private void button4_Click(object sender, RoutedEventArgs e)
        {

            if (!clicked)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = polecenie;
                startInfo.Arguments = argument;
                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process = new Process();
                process.StartInfo = startInfo;
               
                process.EnableRaisingEvents = true;
                process.OutputDataReceived += new DataReceivedEventHandler(ProcessOuputHandler);
                process.ErrorDataReceived += new DataReceivedEventHandler(ProcessOuputHandler);
                process.Exited += new EventHandler(ProcessExitedHandler); 

                process.Start();
                process.BeginOutputReadLine();
                /*    while (!process.HasExited)
                       {
               // Refresh you're WPF window here
                           this.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
                           Thread.Sleep(1000);
                       }*/
                button4.Content = "Stop";
                clicked = true;
            } else {
                process.Kill();
              
            }
            
        }
        private void ProcessExitedHandler(object sendingProcess, EventArgs ew)
        {
            StopMixDelegate stopMixDelegate = new StopMixDelegate(StopProcess);
            button4.Dispatcher.Invoke(DispatcherPriority.Normal, stopMixDelegate);
        }
        private delegate void StopMixDelegate();
        private void StopProcess()
        {
        
            clicked = false;
            button4.Content = "Mix";
        }
        private delegate void ShowLoggingDelegate(string text);
        private static Action EmptyDelegate = delegate() { };
        private void ShowLogging(string text)
        {
            textBox4.AppendText("\n");
            textBox4.AppendText(text);
            textBox4.ScrollToEnd();
        }

        public void ProcessOuputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                
                ShowLoggingDelegate showLoggingDelegate = new ShowLoggingDelegate(ShowLogging);
                textBox4.Dispatcher.Invoke(DispatcherPriority.Normal, showLoggingDelegate, outLine.Data);
                
            }
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            //gen_Command();
        }

        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
           // gen_Command();
        }
    }
    
}
